define(['zepto', 'butterfly/view','../../bsl',
    'text!./file.html'
], function($, View, bsl, file) {
    var me = null;

    var view = View.extend({
        events: {

            // ！！！！ 由于 引入 touch.js 后，tap事件会变成双击，所以 本页面内不能使用tap，用click代替。
            "tap #openDoc" : "openDoc",
            "tap #back" : "back",

      
        },
        render: function() {
            me = this;
        },

        onShow: function() {

            // 绑定自定义事件 滑动切换日历等           
             this.pitem = document.getElementById("pdir");
        this.list = document.getElementById("dcontent");

        // Get root item information
        
        this.updateRootItem();
   

            },
             dir:"",
             root:[],
             current:null,
             parent:null,
             pitem:null,
             list:null,
     htmlItem:'<img class="ficon"></img><div><span class="fname"></span><br/><span class="finf">...</span></div>',
   
   
    updateRootItem:function ( item ) {
        bsl.io.resolveLocalFileSystemURL( dir+item.id, function ( entry ) {
            root.push( entry );
            item.entry = entry;
            updateInf( item, entry );
        }, function ( e ) {
            outLine( "Update "+item.id+" information failed: "+e.message );
        } );
    },

    // Update HTMLUIElement information with entry object
    updateInf:function ( item, entry ) {
        entry.getMetadata( function ( metadata ) {
            var inf = item.querySelector(".finf");
            if ( entry.isDirectory ) {
                inf.innerText = "文件夹:"+metadata.directoryCount+"项，文件:"+metadata.fileCount+"项";
            } else {
                inf.innerText = dateToStr(metadata.modificationTime);
            }
        }, function ( e ) {
            outLine( "Get metadata "+entry.name+" failed: "+e.message );
        }, false );
    },
    // Update ui with entries
    updateList:function ( entries ) {
        var i,items = [].slice.apply(list.querySelectorAll(".fitem"));
        items.shift();
        // sort the entries
        entries.sort( sortCompareEntry )
        // Update item to ui
        for ( i = 0; i < entries.length; i++ ) {
            var di = null;
            if ( i < items.length ) {
                di=items[i];
                di.style.display = "block";
            } else {
                di = document.createElement("div");
                di.className = "fitem";
                di.setAttribute( "onclick", "openDir(this);" );
                di.innerHTML = htmlItem;
                list.appendChild( di );
            }
            di.entry = entries[i];
            di.id = di.entry.name;
            di.querySelector(".fname").innerText = di.id;
            di.querySelector(".finf").innerText = "";
            if ( entries === root ) {
                di.querySelector(".ficon").src = "../images/fdisk.png";
            } else {
                di.querySelector(".ficon").src = di.entry.isDirectory?"../images/fdir.png":"../images/ffile.png";
            }
            updateInf( di, di.entry );
        }
        // Hide other items
        for ( ; i < items.length; i++ ) {
            items[i].style.display = "none";
            items[i].entry = null;
        }
        // Reset scroll offset
        list.scrollTop = 0;
    },
    // Open directory with item(HTMLUIElement)
    openDir:function ( item ) {
        var entry = item.entry;
        if ( !entry ) {
            outSet( "Open directory \""+item.id+"\" with null!" );
            return;
        }
        if ( entry.isDirectory ) {
            outSet( "Open directory: \""+dir+item.id+"\"" );
            var dirReader = entry.createReader();
            dirReader.readEntries( function( entries ) {
                parent = current;
                current = item.entry;
                dir = entry.toURL()+"/";
                // Dispaly up to parent item
                pitem.style.display = "block";
                // Update ui
                updateList( entries );
            }, function ( e ) {
                outLine( "Read directory "+item.id+" failed: "+e.message );
            } );
        } else {
            outSet( "Open file: \""+dir+item.id+"\"" );
            bsl.runtime.openFile( dir+item.id, {}, function ( e ) {
                bsl.nativeUI.alert( "无法打开此文件："+e.emssage );
            } );
        }
    },
    // Back to parent directory
    parentDir:function () {
        outSet( "Go to previous directory: \""+dir+"\"");
        var p = dir.lastIndexOf( "/", dir.length-2 );
        if ( p < 0 || !parent ) { // Up to root
            dir = "";
            current = parent = null;
            // hide up to parent item
            pitem.style.display = "none";
            // Update ui
            updateList( root );
        } else {
            var dirReader = parent.createReader();
            dirReader.readEntries( function( entries ) {
                dir = dir.substr( 0, p+1 );
                outLine( "Current directory: \""+dir+"\"" );
                current = parent;
                current.getParent( function ( entry ) {
                    parent = entry;
                }, function ( e ) {
                    outLine( "Get \""+current.name+"\" parent directory failed: "+e.emssage );
                } );
                parent = null;
                // Update ui
                updateList( entries );
            }, function ( e ) {
                outLine( "Read directory "+item.id+" failed: "+e.message );
            } );
        }
    },
    // Entry sort compare
    sortCompareEntry:function ( a, b ) {
        if ( a.isDirectory && b.isFile ) {
            return -1;
        } else if ( a.isFile && b.isDirectory ) {
            return 1;
        } else {
            return a.name - b.name;
        }
    },
    // Format data to string
    dateToStr:function ( datetime ) {
        var year = datetime.getFullYear(),
        month = datetime.getMonth()+1,
        date = datetime.getDate(),
        hour = datetime.getHours(),
        minutes = datetime.getMinutes(),
        second = datetime.getSeconds();
        if ( month < 10 ) {
            month = "0" + month;
        }
        if ( date < 10 ) {
            date = "0" + date;
        }
        if ( hour < 10 ) {
            hour = "0" + hour;
        }
        if ( minutes < 10 ) {
            minutes = "0" + minutes;
        }
        if ( second < 10 ) {
            second = "0" + second;
        }
        return (year+"-"+month+"-"+date+" "+hour+":"+minutes+":"+second);
    },
              back : function(e){
                window.history.go(-1);
            },
            openDoc:function(){
                 window.butterfly.navigate('com.no5.cordovademo/doc/io', {
                    trigger: true
                });


            },
        
       
             
            });
    return view;
});
